package handler

import (
	"lsgfish/server/internal/service"
	"lsgfish/server/internal/tool"

	"github.com/gin-gonic/gin"
)

// WeatherController 天气控制器
type WeatherController struct{}

// NewWeatherController 创建天气控制器
func NewWeatherController() *WeatherController {
	return &WeatherController{}
}

// GetWeather 获取天气信息
// @Summary 获取天气信息
// @Description 根据用户IP自动获取当前位置的天气信息
// @Tags 天气信息
// @Accept json
// @Produce json
// @Success 200 {object} tool.Response{data=service.WeatherResponse} "获取天气信息成功"
// @Failure 400 {object} tool.Response "参数错误"
// @Failure 500 {object} tool.Response "服务器内部错误"
// @Router /weather/current [get]
func (w *WeatherController) GetWeather(c *gin.Context) {
	// 按优先级尝试不同的方式获取客户端 IP
	ip := c.ClientIP()
	if ip == "" {
		ip = c.GetHeader("X-Real-IP")
	}
	if ip == "" {
		ip = c.GetHeader("X-Forwarded-For")
	}
	if ip == "" {
		ip = c.RemoteIP()
	}

	if ip == "" {
		tool.Err(c, "获取IP失败")
		return
	}

	weatherService := service.NewWeatherService()
	weather, err := weatherService.GetWeatherByIP(ip)
	if err != nil {
		tool.Err(c, "获取天气信息失败:"+err.Error())
		return
	}

	tool.Succ(c, "获取天气信息成功", weather)
}
